import svgCaptcha from 'svg-captcha'
import { getValue, setValue } from '@/config/RedisConfig'
import qs from 'qs'
import SignRecord from '@/model/SignRecord'
import moment from 'moment'

class PublicController {
  constructor() {}
  async getCaptcha(ctx) {
    const body = ctx.request.query
    const newCaptca = svgCaptcha.create({
      size: 4,
      ignoreChars: '0o1il',
      color: true,
      noise: Math.floor(Math.random() * 5),
      width: 150,
      height: 38,
    })
    // 保存图片验证码数据，设置超时时间，单位: s
    // 设置图片验证码超时10分钟
    setValue(body.sid, newCaptca.text, 10 * 60)
    ctx.body = {
      code: 200,
      data: newCaptca.data,
    }
  }
  async getSign(ctx) {
    const body = qs.parse(ctx.request.query)
    const limit = body.limit
    const current = body.current
    let opt = {}
    if (current === '0') {
      opt = moment().format('1000-01-01 00:00:00')
    }
    if (current === '1') {
      opt = moment().format('YYYY-MM-DD 00:00:00')
    }
    if (current === '2') {
      opt = moment().format('1000-01-01 00:00:00')
    }
    const result = await SignRecord.findTime(opt, limit, current)
    ctx.body = {
      code: 200,
      data: result,
    }
  }
}

export default new PublicController()
